#!/usr/bin/env Rscript
################################
## Generalization Error
## estimation. 
## K-fold, context features 
################################



# Flag to send job to cluster or 
# run in a regular computer (still in parallel,
# using MPI backend)
PegasusRun  <- commandArgs()

if(PegasusRun){
  system("bsub < GenErrorkFoldContext.job")
}else{
  #!/usr/bin/env Rscript
  
  ## Load libraries on all cores
  library(plyr)
  library(foreach)
  library(doMPI)
  
  ## Seed RNG
  set.seed(831213)
  
  ## Start cluster & worker loop
  cl  <- startMPIcluster()
  registerDoMPI(cl)
  
  ## Load libraries on master
  library(BayesTree)
  library(glmnet)
  
  ## Load auxiliary functions
  source("./GenErrorAuxFunc.R")
  
  ## Get processed data
  load("./FullData.RData")
  
  
  
  ## Estimate cross-validated generalization errors
  ## Set nr. of folds
  n.folds <- 15
  
  ## Set parameters to best paramsweep values
  bestParams <- read.csv("./BSParamSweep.csv"
                           ,nrows=1
  )[,1:6]
  bartParams  <- expand.grid(k=bestParams$k
                             ,pow=bestParams$pow
                             ,base=bestParams$base
                             ,sigdf=bestParams$sigdf
                             ,sigquant=bestParams$sigquant
                             ,ntree=bestParams$ntree
                             ,sampind=1:n.folds
                             ,burnin=50e3
                             ,niter=5e3
                             ,keepevery=10)
  bartParams  <- as.matrix(bartParams)
  
  ## Estimate generalization errors
  ## for context-only model
  ## using 15-fold CV
  context.errors.boot  <- err.generator(n.folds
                                        ,bartParams
                                        ,all.data$fDataInform
                                        ,all.data$countries.inform
                                        ,parallel=TRUE
                                        ,kfold=TRUE)
  
  
  save(context.errors.boot
       ,file="./GenErrorsKFoldContext.RData")
  
  closeCluster(cl)
  mpi.finalize()
}
